﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Templay;
using System.Xml.Linq;


public class Parser : IParser
{

    public ParseElementCollection Parse(string template)
    {
        ParseElementCollection list = new ParseElementCollection();

        XDocument document = XDocument.Parse(template, LoadOptions.None); // Create xml document

        foreach (XElement element in document.Elements())
            list.Add(CreateParseElement(element));

        return list;
    }

    private ParseElement CreateParseElement(XElement element)
    {
        ParseElement parseElement = new ParseElement(element.Name.LocalName);

        foreach (XAttribute attribute in element.Attributes())
            parseElement.Attributes.Add(attribute.Name.LocalName, new ParseAttribute() { Name = attribute.Name.LocalName, Value = attribute.Value });

        foreach (XElement childElement in element.Elements())
            parseElement.Elements.Add(CreateParseElement(childElement));

        return parseElement;
    }

}
